/* 删除*/
function delA() {
    var tr = this.parentNode.parentNode;
    var name = tr.getElementsByTagName("td")[1].innerHTML;
    var id=tr.getElementsByTagName("td")[1].innerHTML;
    var flag = confirm("确认删除" + name + "吗？");
    if(flag) {
        tr.parentNode.removeChild(tr);
        $.ajax({
            url:"/admin/Serialize/delete",
            type:"post",
            data:{
                id:id,
            },
            dataType:"json",
            success:function(data) {
                console.log(data.success);
                if (data.success) {
                    alert(data.msg);
                    console.log(data.msg);
                } else {
                    alert(data.msg);
                }
            },
            error: function () {

            }
        })
    }
    return false;
};
//获取a，删除
var allA = document.getElementById('employeeTable').getElementsByClassName('delate');
console.log(allA);
for(var i = 0; i < allA.length; i++) {
    allA[i].onclick = delA;
}
//实现全选反选
$("#check").on('click', function() {
    $("tbody input:checkbox").prop("checked", $(this).prop('checked'));
})
$("tbody input:checkbox").on('click', function() {
    //当选中的长度等于checkbox的长度的时候,就让控制全选反选的checkbox设置为选中,否则就为未选中
    if($("tbody input:checkbox").length === $("tbody input:checked").length) {
        $("#check").prop("checked", true);
    } else {
        $("#check").prop("checked", false);
    }
})
/*批量删除*/
$(".delatemore").click(function () {
    var checkNum=$("input[name='user']:checked").length;
    if(checkNum==0){
        alert("至少选择一项");
        return;
    }
    if (confirm("确定要删除所选的项目吗？")){
        var checklist=new Array();
        $("input[name='user']:checked").each(function () {
            var tr=$(this).parent().parent();
            var td=tr.find('td:eq(1)');
            checklist.push(td.text());
        })
        $.ajax({
            url: "/admin/Serialize/deleteMany",
            type: "post",
            data: {
                ids: checklist.toString(),
            },
            dataType: "json",
            success: function (data) {
                if (data.success) {
                    alert(data.msg);
                    window.location.reload();
                } else {

                }
            },
            error: function () {

            }
        })
    }

})
/*推荐*/
$(".detail").click(function () {
    var flag=confirm("确定推荐吗？");
    var tr = this.parentNode.parentNode;
    var id=tr.getElementsByTagName("td")[1].innerHTML;
    if (flag){
        $.ajax({
            url:"/admin/Serialize/recommend",
            type:"post",
            data:{
                id:id,
                number:1,
            },
            dataType:"json",
            success:function (data) {
                if (data.success){
                    alert(data.msg);
                }
            }
        })
    }
})
/*不推荐*/
$(".nodetail").click(function () {
    var flag=confirm("确定取消推荐吗？");
    var tr = this.parentNode.parentNode;
    var id=tr.getElementsByTagName("td")[1].innerHTML;
    if(flag){
        $.ajax({
            url:"/admin/Serialize/recommend",
            type:"post",
            data:{
                id:id,
                number:0,
            },
            dataType:"json",
            success:function(data){
                if (data.success){
                    alert(data.msg);
                    window.location.reload();
                }
            }
        })
    }
})
/*查询 标题*/
$("#search").keyup(function() {
    $("#employeeTable tbody tr ")
        .hide()
        .filter(":contains('" + ($(this).val()) + "')")
        .show();
    $.ajax({
        url:"/admin/Serialize/findAllSerializeByTitle",
        type:"post",
        data:{
            title:name,
        },
        dataType:"json",
        success:function () {
            alert(data.msg);
        },
        error:function () {

        }
    })
});
/*连载类型查找*/
$('#lookfor').click(function() {
    var name=$("#look").val();
    flag=true;
    $("#employeeTable tr").find("td:eq(11)").each(function() {
        if($(this).html()==name){            // 如果找到了
            $(this).parent('tr').siblings().css("display","none");
            flag = false;                    // 将未找到标记设为false
            return false;// 提前终止循环
        }
    });
    if(flag)   {
        alert("查无此连载");
    }                              // 如果没找到就给出提示
    $.ajax({
        url:"/admin/Serialize/findAllSerializeByType",
        type:"post",
        data:{
            tid:name,
        },
        dataType:"json",
        success:function () {

        },
        error:function () {

        }
    })
});
/*返回*/
$("#back").click(function() {
    $("table tbody tr").show();
})
/*修改*/
var arrMove = [];
var _this = null;
$(document).on("click", ".compiler", function() {
    arrMove = []
    _this = $(this).parents("tr")
    $(".movefade").fadeIn(600)
    $(this).parent().siblings().each(function(index) {
        if (index!=0){
            arrMove.push($(this).text())
        }
    })
    $(this).parents().find('.movemodal input').each(function(i) {
        $(this).val(arrMove[i])
    })
})

//修改确定
$(document).on("click", ".addbtn_ok", function() {
    $(".movefade").fadeOut(500)
    var serialize=$("#serialize").val();
    console.log(serialize);
    var user=$("#user").val();
    console.log(user);
    var title=$("#title").val();
    console.log(title);
    var time=$("#time").val();
    console.log(time);
    var love=$("#love").val();
    console.log(love);
    var commit=$("#commit").val();
    console.log(commit);
    var read=$("#read").val();
    console.log(read);
    var collection=$("#collection").val();
    console.log(collection);
    var money=$("#money").val();
    console.log(money);
    var price=$("#price").val();
    console.log(price);
    var vary=$("#type").val();
    console.log(vary);
    $.ajax({
        url:"/admin/Serialize/update",
        type:"post",
        data:{
            sId:serialize,
            uId:user,
            sTitle:title,
            sPraise:love,
            sComment:commit,
            sRead:read,
            sCollection:collection,
            sContribute:money,
            sPrice:price,
            tId:vary,
        },
        dataType:"json",
        success:function (data) {
            if(data.success){
                alert("修改成功");
                location.reload()
            }else{
                alert(data.msg);
            }
        },
        error:function (data) {
            alert(data.msg);
            console.log(data.msg);
        }
    })
})
//修改取消
$(document).on("click", ".addbtn_no", function() {
    $(".movefade").fadeOut(500)
})

$(document).on("click", ".modal-header i", function() {
    $(".movefade").fadeOut(500)
})
/*状态*/
var sta=$("#employeeTable tr").find("td:eq(12)");
for (var i=0;i<=sta.length;i++){
    console.log($(sta[i]).text())
    if ($(sta[i]).text() == 1) {
        $(sta[i]).text("已推荐");
    } else if ($(sta[i]).text() == 0) {
        $(sta[i]).text("未推荐");
    }
}